const app = getApp();
var util = require('../../utils/util.js');
Page({
    data: {
        equipmentList:[],
        faultEquipmentList:[],
        imgUrl:app.globalData.APIURL + '/img/',
        tabs: ["我的设备", "故障设备"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0
    },

    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '工作台'
        });
        this.getEquipmentList();
        this.getFaultEquipmentList();

    },
    onPullDownRefresh: function () {
        //下拉刷新 获取列表，重置当前页码
        this.getEquipmentList()
    },
    equipmentActivation: function(){
        var that = this;
        wx.scanCode({
            success: (res) => {
                //可供多个参数截取
                var url = res.result.slice(res.result.indexOf( "?" ));
                var theRequest = {};
                console.log(url);
                if ( url.indexOf( "?" ) != -1 ) {
                    var str = url.substr( 1 ); //substr()方法返回从参数值开始到结束的字符串；
                    var strs = str.split( "&" );
                    for ( var i = 0; i < strs.length; i++ ) {
                        theRequest[ strs[ i ].split( "=" )[ 0 ] ] = ( strs[ i ].split( "=" )[ 1 ] );
                    }
                }
                var imei = theRequest.imei;
                wx.navigateTo({
                    url: '/pages/equipment-detail-inactivated/equipment-detail-inactivated?imei='
                        + imei

                })
                console.log(imei)
            },
            fail: (res) => {
                console.log(res);
            }
        })
    },
    getInactiveEquip:function(imei){
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/getInactiveEquip',
            data: {
                token: app.globalData.token,
                imei: imei,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                // 停止刷新
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    let equipment = JSON.stringify(res.data.equipment);
                    let products = JSON.stringify(res.data.products);
                    let tracks = JSON.stringify(res.data.tracks);
                    wx.navigateTo({
                        url: '/pages/equipment-detail-inactivated/equipment-detail-inactivated?imei='
                            + equipmentInfo
                            + '&products=' + products
                            + '&tracks=' + tracks
                    })
                }

            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    },
    getEquipmentList: function () {
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/getMyEquipShortage',
            data: {
                token: app.globalData.token,
                type: 1,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                // 停止刷新
                wx.stopPullDownRefresh();
                wx.hideLoading();
                if(app.requestEro(res.data.success,res.data.msg)){
                    console.log(res.data)
                    that.setData({
                        equipmentList: res.data.shortages
                    })
                }

            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    },
    qrcodeScan: function () {
        wx.scanCode({
            success: (res) => {
                //可供多个参数截取
                var url = res.result.slice(res.result.indexOf( "?" ));
                var theRequest = {};
                console.log(url);
                if ( url.indexOf( "?" ) != -1 ) {
                    var str = url.substr( 1 ); //substr()方法返回从参数值开始到结束的字符串；
                    var strs = str.split( "&" );
                    for ( var i = 0; i < strs.length; i++ ) {
                        theRequest[ strs[ i ].split( "=" )[ 0 ] ] = ( strs[ i ].split( "=" )[ 1 ] );
                    }
                }
                var imei = theRequest.imei;
                wx.navigateTo({
                    url:"/pages/equipment-detail/equipment-detail?imei="
                        + imei

                })
                console.log(imei)
            },
            fail: (res) => {
                console.log(res);
            }
        })
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    getFaultEquipmentList: function () {
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/getFaultEquipment',
            data: {
                token: app.globalData.token,
                type: 0,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                // 停止刷新
                wx.stopPullDownRefresh();
                wx.hideLoading();
                if(app.requestEro(res.data.success,res.data.msg)){
                    console.log(res.data)
                    that.setData({
                        faultEquipmentList: res.data.equipments
                    })
                }

            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    },



})
;